package com.lucy.property.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.property.constant.HouseShipConst;
import com.lucy.property.constant.HouseStatusConst;
import com.lucy.property.entity.HouseShip;
import com.lucy.property.entity.UnitHouse;
import com.lucy.property.module.fee.FeeItem;
import com.lucy.property.params.HouseShipCreateParams;
import com.lucy.property.params.HouseShipQueryParams;
import com.lucy.property.params.HouseShipUpdateParams;
import com.lucy.property.service.HouseShipService;
import com.lucy.property.service.UnitHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 * 房产关联管理
 */
@RestController
@RequestMapping("house")
@Api(tags = "房产关联管理")
public class HouseShipController {
    @Autowired
    HouseShipService service;

    @Autowired
    UnitHouseService houseService;

    @PostMapping("ship")
    @ApiOperation(value = "房产关联新建",notes = "")
    public ResponseDTO<Object> shipCreate(@RequestBody @Validated HouseShipCreateParams params){
        HouseShip ship = new HouseShip();
        BeanUtil.copyProperties(params,ship, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        // 更新
        Optional<UnitHouse> houseOp = houseService.getOne(params.getHouseId());
        if (!houseOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND,"房产信息不存在");
        }
        UnitHouse house = houseOp.get();
        house.setHouseStatus(HouseStatusConst.IN_USE);
        house.setLiveNum(house.getLiveNum() + 1);
        houseService.updateOne(house);
        // 写入
        return ResponseDTO.succData(
                service.saveOne(ship)
        );
    }


    @DeleteMapping("ship/{id}")
    @ApiOperation(value = "房产关联删除",notes = "")
    public ResponseDTO<Object> shipDelete(@PathVariable("id") String id){
        // 获取关联信息
        Optional<HouseShip> shipOp = service.getOne(id);
        if (!shipOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        HouseShip ship = shipOp.get();
        // 获取房产id
        Optional<UnitHouse> houseOp = houseService.getOne(ship.getHouseId());
        if (!houseOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        UnitHouse house = houseOp.get();
        // 获取和房产信息关联的
        HouseShip ship1 = new HouseShip();
        ship1.setHouseId(ship.getHouseId());
        Example<HouseShip> example = Example.of(ship1);
        List<HouseShip> shipList = service.getExample(example);
        if (shipList.size() == 1){
            house.setHouseStatus(HouseStatusConst.IN_FREE);
            house.setLiveNum(0);
        }else {
            house.setLiveNum(shipList.size() - 1);
        }
        houseService.updateOne(house);
        service.deleteOne(id);
        //
        return ResponseDTO.succMsg("删除成功");
    }

    @PutMapping("ship")
    @ApiOperation(value = "房产关联更新",notes = "")
    public ResponseDTO<Object> shipUpdate(@RequestBody @Validated HouseShipUpdateParams params){
        Optional<HouseShip> shipOp = service.getOne(params.getId());
        if (!shipOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.NOT_FOUND);
        }
        HouseShip ship = shipOp.get();
        BeanUtil.copyProperties(params,ship,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                service.updateOne(ship)
        );
    }

    @GetMapping("ship")
    @ApiOperation(value = "房产关联列表",notes = "")
    public ResponseDTO<Object> shipListQuery(HouseShipQueryParams params){
        Sort sort;
        if("DESC".equalsIgnoreCase(params.getOrder())){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);
        /*
        构建 Example
         */
        HouseShip item = new HouseShip();
        BeanUtil.copyProperties(params,item,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<HouseShip> example = Example.of(item);
        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("ship/{id}")
    @ApiOperation(value = "房产关联查询",notes = "")
    public ResponseDTO<Object> shipQuery(@PathVariable("id") String id){
        Optional<HouseShip> itemOp = service.getOne(id);
        return itemOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }
}
